
var sys_permission = $("#sys_permission").val();//首页模块
var permission  = JSON.parse(sys_permission);
var sysuser = $("#sysuser").val();//用户信息
var user = JSON.parse(sysuser);
var element;
var $;

layui.use(['element','layer','form'], function(){
	leftModelRender(permission, user);//初始化首页左侧和顶部导航栏
	element = layui.element;
	element.init();
	
	/* 点击重置密码界面 */
	$("#resetPassword").click(function() {
		layer.open({
			type : 1,
			title : "重置密码",
			skin : "myclass",
			area : [ "20%" ],
			btn : [ '重置密码', '取消' ],
			content : $("#resetPasswordDiv"),
			success : function(layero) {
				document.getElementById("addEmployeeForm").reset();//清空form表单的值 
				var btn = layero.find('.layui-layer-btn');
				btn.css('text-align', 'center');
			},
			yes : function(index, layero) {
				//限制输入框中不能用空值
			    var id = user.id;
				var password = $('#password').val();
				var password1 = $('#password1').val();
			     var num=0;
			     $("input[type$='password']").each(function(n){
			    	 if($(this).val()==""){
				    	num++
			    	 }		         
			     });
			     if(num>0){
			    	 layer.msg('密码不能为空');
			    	 return false;
			     } else if(password != password1){
			    	 layer.msg('两次输入密码必须一致');
			    	 return false;
			     }
				$.ajax({
					type : "POST",//方法类型
					dataType : "json",//预期服务器返回的数据类型
					url : "sysuser/userAdd",//url
					data : {
						id : id,
						password : password,
						username : user.username
					},
					success : function(result) {
						if (result.code == 0) {
							layer.close(index);
							layer.msg('密码修改成功');
							window.location.href="logout";
						}						;
					},
					error : function() {
						layer.msg('密码修改失败');
						layer.close(index);
					}
				});
			}
		});
	});
	showRightContent("25","业务待办","backstage/projectManagement/project_details.html")
});

$("#notice").click(function() {
	$.ajax({
		type : "POST",//方法类型
		dataType : "json",//预期服务器返回的数据类型
		url : "../../dictionary_item/dictionaryQuery?dictionaryType=systemNotice",//url
		success : function(result) {	        
			var noticeText = '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight:300;width:300px;word-wrap:break-word;">'
								+ "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" +result.dictionary.text + 
								'</div>';
			// 一个公告层
			layer.open({
				type : 1,
				title : false, // 不显示标题栏
				closeBtn : false,
				area : '%30',
				shade : 0.8,
				id : 'LAY_layuipro', // 设定一个id，防止重复弹出
				btn : [ '知悉', '关闭' ],
				btnAlign : 'c',
				moveType : 1, // 拖拽模式，0或者1
				content : noticeText,
			});
		}
	});
});


var active = {
  tabAdd: function(id, title, href){
    //新增一个Tab项
    element.tabAdd('rightMenu', {
      title: title,
      content:'<iframe data-frameid="'+id+'" frameborder="0" name="content" scrolling="no" width="100%" src="' + href + '"></iframe>',
      id:id
    })
    FrameWH();
  }
  ,tabDelete: function(id){
    //删除指定Tab项
    element.tabDelete('rightMenu', id); 
    othis.addClass('layui-btn-disabled');
  }
  ,tabChange: function(id){
    //切换到指定Tab项
    element.tabChange('rightMenu', id);
    $("iframe[data-frameid='"+id+"']").attr("src",$("iframe[data-frameid='"+id+"']").attr("src"))//切换后刷新框架
  }
};

//初始化首页左侧和顶部导航栏
function leftModelRender(permissionArry, user){
	var permission;
	var permission_id;
	var count = 0;
	$('#name').append(user.name);//用户姓名
	for(i = 0; i < permissionArry.length; i++){
		permission = permissionArry[i];
		if(permission.type == 1){
			if(count==0){
				count++;
				permission_id = permission.id;
				$('#topMenu').append("<li class='layui-nav-item layui-this'><a href='javascript:findLaySide(" + permission.id + ")'>" + permission.title + "</a></li>");
			}else{
				$('#topMenu').append("<li class='layui-nav-item'><a href='javascript:findLaySide(" + permission.id + ")'>" + permission.title + "</a></li>");	
			}
			
		}
	}
	
	count=0;
	for(j = 0; j < permissionArry.length; j++){
		if(permissionArry[j].type == 2 && permissionArry[j].parent_id == permission_id){
			if(permissionArry[j].href == "" || permissionArry[j].href == null){
				if(count == 0){
					count++;
					$('#navTree').append("<li class='layui-nav-item layui-nav-itemed'><a class='' href='javascript:;'>"+ permissionArry[j].title +"</a><dl class='layui-nav-child' id='"+ permissionArry[j].id +"'></dl></li>");
				} else {
					$('#navTree').append("<li class='layui-nav-item'><a class='' href='javascript:;'>"+ permissionArry[j].title +"</a><dl class='layui-nav-child' id='"+ permissionArry[j].id +"'></dl></li>");
				}
				
			}else{
				$('#navTree').append("<li class='layui-nav-item' data-url='"+ permissionArry[j].href +"'><a class='' href='javascript:;'>"+ permissionArry[j].title +"</a></li>");
			}				
		}
	}
	for(k = 0; k < permissionArry.length; k++){
		if(permissionArry[k].type==3){
			$("#"+ permissionArry[k].parent_id +"").append("<dd><a href='javascript:showRightContent("+ permissionArry[k].id +",&quot;"+ permissionArry[k].title +"&quot;,&quot;"+ permissionArry[k].href +"&quot;);' data-url='"+ permissionArry[k].href +"'>"+ permissionArry[k].title +"</a></dd>");
		}			    	
    }
    
}

//左侧菜单栏点击事件
function findLaySide(permission_id){
	$('#navTree').html("");
	var count=0;
	for(j = 0; j < permission.length; j++){
		if(permission[j].type == 2 && permission[j].parent_id == permission_id){
			if(permission[j].href == "" || permission[j].href == null){
				if(count == 0){
					count++;
					$('#navTree').append("<li class='layui-nav-item layui-nav-itemed'><a class='' href='javascript:;'>"+ permission[j].title +"</a><dl class='layui-nav-child' id='"+ permission[j].id +"'></dl></li>");
				} else {
					$('#navTree').append("<li class='layui-nav-item'><a class='' href='javascript:;'>"+ permission[j].title +"</a><dl class='layui-nav-child' id='"+ permission[j].id +"'></dl></li>");
				}
				
			}else{
				$('#navTree').append("<li class='layui-nav-item' data-url='"+ permission[j].href +"'><a class='' href='javascript:;'>"+ permission[j].title +"</a></li>");
			}				
		}
	}
	for(k = 0; k < permission.length; k++){
		if(permission[k].type==3){
			$("#"+ permission[k].parent_id +"").append("<dd><a href='javascript:showRightContent("+ permission[k].id +",&quot;"+ permission[k].title +"&quot;,&quot;"+ permission[k].href +"&quot;);' data-url='"+ permission[k].href +"'>"+ permission[k].title +"</a></dd>");
		}			    	
    }
	element.init();
}

//调用tab方法
function showRightContent(id,title,herf){
	//逻辑判断 
	if ($(".layui-tab-title li[lay-id]").length <= 0) {
        active.tabAdd(id, title, herf);
    } else {
        var isData = false;
        $.each($(".layui-tab-title li[lay-id]"), function () {
            if ($(this).attr("lay-id") == id) {
                isData = true;
            }
        })
        if (isData == false) {
        	 active.tabAdd(id, title, herf);
        }
    }
    active.tabChange(id);
}

//获取窗口的高度
function FrameWH() {
    var h = $(window).height() -165;
    $("iframe").css("height",h+"px");
}
$(window).resize(function () {
    FrameWH();
})